open BUFF,"<merge_expr_filtered" or die "no file exists merge_expr_filtered\n";
open BUFF1,"<combined_GBM_normal_plink_final_filetered_het_minhomo_10.tped" or die "no file exists combined_GBM_normal_plink_final_filetered_het_minhomo_10.tped\n";
$samplesize = 20;
#reading first line of expr file
$line = <BUFF>;
chomp($line);
#create sample array expr
@gene_samples = split(/\t/,$line);
#shift(@gene_samples);
$length = @gene_samples;
#print $length."\n";
@value=split(/\./,$length/$samplesize);
#extracting the integer part
$value = $value[0];

if($value < 1)
{
	$value = 1;
}
#read the first line in tped file for sample names
$line = <BUFF1>;
chomp($line);
#create sample array
@geno_samples = split(" ",$line);
my @group_expr;
my @group_geno;
$j =0;
#create gene expression and geno typing sub part file names 
for($i=0;$i<$value;$i++)
{
	$val = $i+1;
	$group_expr_main[$i] = "expr_main_$val";
	$group_geno_main[$i] = "geno_main_$val";
	$group_expr[$i] = "expr_part_$val";
	$group_geno[$i] = "geno_part_$val";
	$buff = "WREXPR$val";
	open($buff,">./temp/$group_expr[$i]");
	$buff = "WREXPRMAIN$val";
	open($buff,">./temp/$group_expr_main[$i]");
	$buff = "WRGENO$val";
	open($buff,">./temp/$group_geno[$i]");
	$buff = "WRGENOMAIN$val";
	open($buff,">./temp/$group_geno_main[$i]");
}

$prev_llimit = -20;
$prev_ulimit = 0;
for($i=0;$i<$value;$i++)
{
	$val = $i+1;
	$buff = "WREXPR$val";
	$buff1 = "WRGENO$val";
	$buff2 = "WREXPRMAIN$val";
	$buff3 = "WRGENOMAIN$val";
	#for($j=0;$j<$value;$j++)
	#{
	$llimit = $prev_llimit+20;
	$ulimit = $prev_ulimit+20;
	if($i == $value-1)
	{
		$ulimit = @gene_samples;
	}
	#print "$llimit\t$ulimit\n";
	for($k=0;$k<@gene_samples;$k++)
	{
		if($k >= $llimit && $k < $ulimit)
		{
			print  $buff "\t".$gene_samples[$k];
        		print  $buff1 " ".$geno_samples[$k];
		}
		else
		{
			print   $buff2 "\t".$gene_samples[$k];
			print	$buff3 " ".$gene_samples[$k];
		}		
	}
	print   $buff "\n";
	print   $buff1 "\n";
	print   $buff2 "\n";
	print   $buff3 "\n";
	$prev_llimit =$llimit;
	$prev_ulimit =$ulimit;
	#}
}

#split each gene in to sub part files
while($line = <BUFF>)
{
	chomp($line);
	@genes = split("\t",$line);
	$prev_llimit = -20;
	$prev_ulimit = 0;
	$geneid = shift(@genes);
	for($i=0;$i<$value;$i++)
	{
        	$val = $i+1;
        	$buff = "WREXPR$val";
        	$buff2 = "WREXPRMAIN$val";
        	$llimit = $prev_llimit+20;
        	$ulimit = $prev_ulimit+20;
		print $buff2 $geneid;
		print $buff $geneid;
        	if($i == $value-1)
        	{
                	$ulimit = @genes;
        	}
        	#print "$llimit\t$ulimit\n";
        	for($k=0;$k<@genes;$k++)
        	{
                	if($k >= $llimit && $k < $ulimit)
                	{
                        	print  $buff "\t".$genes[$k];
                	}
                	else
                	{
                        	print   $buff2 "\t".$genes[$k];
                	}
        	}
       		print   $buff "\n";
        	print   $buff2 "\n";
        	$prev_llimit =$llimit;
        	$prev_ulimit =$ulimit;
        
	}

}

#split each genotype in to sub part files
while($line = <BUFF1>)
{
	chomp($line);
        @genotypes = split("\t",$line);
	$prev_llimit = -20;
	$prev_ulimit = 0;
	$kk1 = shift(@genotypes);
	$kk2 = shift(@genotypes);
	$kk3 = shift(@genotypes);
	$kk4 = shift(@genotypes);
	$kk5 = shift(@genotypes);
	$kk6 = shift(@genotypes);
	$kk7 = shift(@genotypes);
	for($i=0;$i<$value;$i++)
	{
        	$val = $i+1;
        	$buff1 = "WRGENO$val";
        	$buff3 = "WRGENOMAIN$val";
        	$llimit = $prev_llimit+20;
        	$ulimit = $prev_ulimit+20;
		print $buff1 "$kk1 $kk2 $kk3 $kk4 $kk5 $kk6 $kk7";
		print $buff3 "$kk1 $kk2 $kk3 $kk4 $kk5 $kk6 $kk7";
        	if($i == $value-1)
        	{
                	$ulimit = @genotypes;
        	}
        	for($k=0;$k<@genotypes;$k++)
        	{
                	if($k >= $llimit && $k < $ulimit)
                	{
                        	print  $buff1 " ".$genotypes[$k];
                	}
                	else
                	{
                        	print   $buff3 " ".$genotypes[$k];
                	}
        	}
        	print   $buff1 "\n";
        	print   $buff3 "\n";
        	$prev_llimit =$llimit;
        	$prev_ulimit =$ulimit;
	}

}
for($i=1;$i<$value+1;$i++)
{
	$buff = "WREXPR$i";
	$buff1 = "WRGENO$i";
	close($buff);
	close($buff1);
}
